package org.deng.learn.mybatis.xml;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

/**
 * @Author DengLibin
 * @Date 19-4-27
 * @Time 下午6:57
 * @Desc 单例工厂
 */
public class SingletonFactory {

    private static class Inner{
        private Inner(){}
        private static SqlSessionFactory sessionFactory;
        static {
            try {
                System.out.println("创建SqlSessionFactory");
                Reader reader = Resources.getResourceAsReader("mybatis/mybatis.cfg.xml");
                sessionFactory =   new SqlSessionFactoryBuilder().build(reader);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        public static  SqlSessionFactory getSqlSessionFactory(){
            return sessionFactory;
        }
    }

    /**
     * 获取sqlSessionFactory
     * @return
     */
    public static SqlSessionFactory getSqlSessionFactory(){
        return Inner.getSqlSessionFactory();
    }
}
